package com.srd.phone.bean;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class BillDetail {

    public int id;
    public int state;
    public String symbol;
    public String nm;
    public String stat;
    public double moneys;
    public String stime;
    public int numbers;
    public int mid;
    public double anyield;
    public int term;
    public int num;
    public int style;
    public double profit;
    public int uid;
    public String numbersm;
    public String statem;
    public String statm;
    public int yrepay;
    public int wrepay;
    public double mCount;
    public List<HPayment> list = null;

    public static BillDetail parseData(String json){

        BillDetail billDetail = new BillDetail();
        try {
            JSONObject jsonObj = new JSONObject(json);
            JSONObject jsonObject = jsonObj.optJSONObject("tRecordInfo");
            billDetail.nm = jsonObject.optString("nm");  // 标题
            billDetail.moneys = jsonObject.optDouble("moneys",0);  // 借款总额
            billDetail.profit = jsonObject.optDouble("profit",0);  // 收益
            BigDecimal count = new BigDecimal(billDetail.moneys).add(new BigDecimal(billDetail.profit));
            billDetail.mCount = count.doubleValue();  // 本息合计
            billDetail.term = jsonObject.optInt("term");  // 借款期数
            billDetail.anyield = jsonObject.optDouble("anyield",0);  // 年利率
                                                                  //  每期还款
            billDetail.yrepay = jsonObject.optInt("yrepay");   // 已还期数
            billDetail.wrepay = jsonObject.optInt("wrepay");   // 未还期数

            List<HPayment> list = new ArrayList<>();
            JSONArray jsonArray = jsonObj.optJSONArray("hPayment");
            for (int i = 0; jsonArray != null && i < jsonArray.length(); i++) {
                HPayment item = new HPayment();
                JSONObject object = jsonArray.getJSONObject(i);
                item.num = object.optInt("num");   // 期数
                item.pstatem = object.optString("pstatem");  // 状态 说明
                item.ptime = object.optString("ptime");   // 还款时间
                                                            // 回收本金   回收利息
                list.add(item);
            }
            billDetail.list = list;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return billDetail;
    }
}
